import axios from 'axios'
// import api from '../../api/index.js'

export default async (url = '', data = null, type = 'GET', get = "", turn = false, returnParam = {}) => {
	type = type.toUpperCase();
	// let url_base = window.api.mall_base_host + url;
	// url = window.api.local_host + url;

	// let url_base = "http://172.19.6.152:33001" + url;
	let url_base = window.api.customer_host + url;
	let date = new Date().getTime();
	let sess = window.localStorage.getItem('sessionId');

	if (turn) {
		// url = "http://172.19.6.152:33003" + url;
		if (turn.config === 1) {
			url = api.base_host + url;
		} else {
			url = window.api.trade_host + url;
		}


	} else {
		// url = "http://172.19.6.152:33004" + url;
		url = window.api.customer_host + url;
		// url = "http://172.19.5.89:33004" + url;
	}
	//
	if (type == 'GET') {
		if (data == null) {
			url = url + '?token=' + date;

		} else {

			if (get == 'method') {
				url = url_base + '?' + data;
			} else if (get == 'other') {
				url = url + '?' + data + '&token=' + date;
			} else {
				url = url + data ;
			}
		}



		let config = {
			url: url,
			headers: { 'sessionId': sess, 'Content-Type': 'application/json', 'Accept': 'application/json' },
			timeout:8000,
		}
		try {
			const response_get = await axios(config);
			if (returnParam.type == 1) {
				if (response_get.status === 200) {
					return response_get;
				} else {
					Toast({
						message: "网络错误！",
						type: "fail",
						duration: api.alertTime_2
					})
					return
				}
			}
			return response_get.data
		} catch (error) {
			return error
		}

	}

	if (type == 'POST') {
		let config = {
			url: url+'?token=' + date,
			method: 'post',
			data: JSON.stringify(data),
			headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', 'sessionId': sess },
			timeout:8000,
		}

		try {
			const response_post = await axios(config);
			if (returnParam.type == 1) {
				if (response_post.status === 200) {
					return response_post;
				} else {
					Toast({
						message: "网络错误！",
						type: "fail",
						duration: api.alertTime_2
					})
					return
				}
				// return response_post;
			}
			return response_post.data;
		} catch (error) {
			console.log('error', error);
			return error
		}
	}


	if (type == "PUT") {
		let config = {
			url: url,
			method: 'put',
			data: JSON.stringify(data),
			headers: { 'sessionId': sess, 'Content-Type': 'application/json', 'Accept': 'application/json' },
			timeout:8000,
		}
		try {
			const response_post = await axios(config);
			return response_post.data
		} catch (error) {
			throw new Error(error)
		}
	}

}
